package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.LaborContract;
import com.wn55.hrms.service.ContractService;
import com.wn55.hrms.service.vo.ContractVo;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author henry
 */

@RestController
@RequestMapping("contract")
@Api(value = "此接口用于操作劳动合同管理功能")
public class ContractController {

    private ContractService contractService;

    @Autowired
    public void setContractService(ContractService contractService){this.contractService=contractService;}

    /**
     * 分页查询所有合同数据
     * @return
     */
    @GetMapping("/loadMenu")
    @ApiOperation(value = "分页查询合同数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "int",name="now",value = "当前页",defaultValue = "1"),
            @ApiImplicitParam(paramType = "query",dataType = "int",name="size",value = "每页条数",defaultValue = "2"),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="empName",value = "员工姓名模糊查询")
    })
    public ResultResponse<PageInfo<ContractVo>> initContractList(
            @RequestParam(value = "now",defaultValue = "1") Integer now ,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            @RequestParam(value = "empName",required = false)String name){
        ResultResponse<PageInfo<ContractVo>> result;
        try {
            PageInfo<ContractVo> pageInfo = contractService.initContract(now,size,name);
            result = new ResultResponse<>(200,"查询合同成功!",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询合同失败!");
        }
        return result;
    }

    /**
     * 添加新合同
     * @param laborContract
     * @return
     */
    @PostMapping("addContract")
    @ApiOperation("新增合同")
    public ResultResponse<Void> addContract(@RequestBody LaborContract laborContract){
        ResultResponse<Void> result = null;
        try {
            contractService.add(laborContract);
            result = new ResultResponse<>(200,"新增合同成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"新增合同失败!");
        }
        return result;
    }

    @PutMapping("/modifyContract")
    @ApiOperation("修改合同")
    public ResultResponse<Void> modifyContract(@RequestBody LaborContract laborContract){
        ResultResponse<Void> result = null;
        try {
            contractService.update(laborContract);
            result = new ResultResponse<>(200,"修改合同成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改合同失败!");
        }

        return result;
    }


    @DeleteMapping("deleteContract/{id}")
    @ApiOperation("根据id删除合同")
    @ApiImplicitParam(paramType = "path",dataType = "int",name="id",value = "合同id")
    public ResultResponse<Void> deleteContract(@PathVariable("id") int id){
        ResultResponse<Void> result = null;
        try {
            contractService.delete(id);
            result = new ResultResponse<>(200,"删除合同成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除合同失败!");
        }
        return result;
    }



}
